package cn.jiadakeji.parent.config.security.handle;

import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author hhaoyu
 */
@Component
public class PermitPathSupport {

    private final List<String> permitPaths;

    private final AntPathMatcher antPathMatcher;


    //不验证
    public PermitPathSupport() {
        permitPaths = new ArrayList<>();
        antPathMatcher = new AntPathMatcher();
        permitPaths.add("/profile/**");
        permitPaths.add("/common/download**");
        permitPaths.add("/common/download/resource**");
        permitPaths.add("/doc.html");
        permitPaths.add("/swagger-resources/**");
        permitPaths.add("/webjars/**");
        permitPaths.add("/*/api-docs");
        permitPaths.add("/druid/**");
        permitPaths.add("/captchaImage");
        permitPaths.add("/login/**");
        permitPaths.add("/weiXinPay/**");
    }

    /**
     * 请求地址是否匹配
     *
     * @param requestPath 请求地址
     * @return 是/否
     */
    public boolean match(String requestPath) {
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        for (String url : permitPaths) {
            url = request.getContextPath().concat(url);
            if (antPathMatcher.match(url, requestPath)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 许可地址列表
     *
     * @return 许可地址列表
     */
    public List<String> permitPaths() {
        return permitPaths;
    }
}
